<?php
/**
 * EntryManager(入职审核)
 */
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class EntryManagerController extends AdminbaseController
{
    //申请入职model
    protected $apply_job_model;
    //申请入职人员model
    protected $person_model;
    //收入信息model
    protected $income_model;
    //企业model
    protected $coms_model;
    //驻厂与企业绑定model
    protected  $user_bind_com_model;
    public function _initialize()
    {
       parent::_initialize();
       $this->apply_job_model = D("Common/ApplyJob");
       $this->person_model= D("Common/Person");
       $this->income_model = D("Common/Income");
       $this->coms_model =  D("Common/Coms");
       $this->user_bind_com_model = D("Common/UserBindCom");
    }

    // 申请入职列表
    public function index()
    {
        $job_state = I('request.job_state','');
        parent::dict_select(array(array(
            'code' => 'PERSON_APPLY_STATE',
            'name' => 'job_state',
            'text' => '入职状态',
            'value' => $job_state
        )
        ));
        $where = array();
        $where['applyjob.deleted_time']  = array('EXP','IS NULL');   /**不查询已删除的用户*/
        if($job_state){
            $where['applyjob.job_state']=$job_state;
        }
        $is_zhuchang = get_is_zhuchang_admin();
        $formget = array();
        $com_name = I('request.com_name');
        if($com_name){
            $where['coms.com_name'] = array('like',"%$com_name%");
            $formget['com_name'] = $com_name;
        }
        $invite_name = I('request.invite_name');
        if($invite_name){
            $where['invite.name'] = array('like',"%$invite_name%");
            $formget['invite_name'] = $invite_name;
        }
        $created_time =  I('request.created_time');
        if($created_time){
            $formget['created_time'] = $created_time;
            $where["FROM_UNIXTIME( applyjob.created_time, '%Y-%m-%d')"] =$created_time;
        }

        if(get_current_admin_id()!=1){
            $where['user_bind_model.user_id'] = get_current_admin_id();
            $count = $this->apply_job_model
                ->join('as applyjob LEFT JOIN __USER_COM__ as user_bind_model ON user_bind_model.com_id = applyjob.com_id')
                ->join('LEFT JOIN __PERSON__ as person ON applyjob.person_id = person.id')
                ->join('LEFT JOIN __COMS__ as coms ON applyjob.com_id = coms.id')
                ->join('LEFT JOIN __INVITE__ as invite ON applyjob.invite_id = invite.uid')
                ->where($where)
                ->order("applyjob.created_time DESC")
                ->count();

           // $sql = M()->getLastSql();
        }else{
            $count = $this->apply_job_model
                ->join('as applyjob LEFT JOIN __PERSON__ as person ON applyjob.person_id = person.id')
                ->join('LEFT JOIN __COMS__ as coms ON applyjob.com_id = coms.id')
                ->join('LEFT JOIN __INVITE__ as invite ON applyjob.invite_id = invite.uid')
                ->where($where)
                ->order("applyjob.created_time DESC")
                ->count();
        }

        $page = $this->page($count, 20);
        $file_arr = array(
            'applyjob.id',
            'coms.id as com_id',
            'person.id as person_id',
            'applyjob.invite_id',
            'applyjob.job_state',
            'coms.com_name',
            'coms.province',
            'coms.city',
            'coms.district',
            'coms.com_give',
            'person.name as person_name',
            'applyjob.created_time',
            'applyjob.last_time',
        	'person.mobile',
            'person.card',
            'invite.name as invite_name'
        );
        if(get_current_admin_id()!=1){
            $list = $this->apply_job_model->field(implode(",",$file_arr))
                ->join('as applyjob LEFT JOIN __USER_COM__ as user_bind_model ON user_bind_model.com_id = applyjob.com_id')
                ->join('LEFT JOIN __PERSON__ as person ON applyjob.person_id = person.id')
                ->join('LEFT JOIN __COMS__ as coms ON applyjob.com_id = coms.id')
                ->join('LEFT JOIN __INVITE__ as invite ON applyjob.invite_id = invite.uid')
                ->where($where)
                ->order("applyjob.created_time DESC")
                ->limit($page->firstRow . ',' . $page->listRows)
                ->select();
          // $sql = M()->getLastSql();
        }else{
            $list = $this->apply_job_model->field(implode(",",$file_arr))
                ->join('as applyjob LEFT JOIN __PERSON__ as person ON applyjob.person_id = person.id')
                ->join('LEFT JOIN __COMS__ as coms ON applyjob.com_id = coms.id')
                ->join('LEFT JOIN __INVITE__ as invite ON applyjob.invite_id = invite.uid')
                ->where($where)
                ->order("applyjob.created_time DESC")
                ->limit($page->firstRow . ',' . $page->listRows)
                ->select();
           // $sql = M()->getLastSql();
        }
        $this->assign('list', $list);
        $this->assign("page", $page->show('Admin'));
        $this->assign("formget", $formget);
        $this->display();
    }

    /**
     * 修改入职状态
     * 状态类型参考数字字典PERSON_APPLY_STATE
     */
    public function changer_state()
    {
        $id = I("get.id",0,'intval');
        $job_state  = I("get.job_state",0,'intval');
        if($job_state==0||$id==0){
            $this->error("获取用户状态或ID失败，请联系管理员");
        }
        //开启事务
        M()->startTrans();
        //修改申请记录状态
        $this->apply_job_model->last_time=time();
        $this->apply_job_model->updated_time=time();
        $this->apply_job_model->job_state=$job_state;
        $this->apply_job_model->adminID=get_current_admin_id();
        if($job_state==3){
        	$this->apply_job_model->injob_time = time(); //更新入职时间
        }
        $rs= $this->apply_job_model->where('id='.$id)->save();
        if($rs!==false){
            //确认入职进行财务操作
           if($job_state==3){
                $app_job_info = $this->apply_job_model->find($id);
                $com_info = $this->coms_model->field('id,com_name,com_give')->find($app_job_info['com_id']);
                $person_info = $this->person_model->field('id,name')->find($app_job_info['person_id']);
                $msg=get_current_admin_name().'确认'.$person_info['name'].'入职'.$com_info['com_name'].'获取金额'.$com_info['com_give'].'元';
                if(empty($app_job_info)||empty($com_info)||empty($person_info)||empty($msg)
                    ||empty($com_info['com_give'])||empty($id)){
                    M()->rollback();
                    $this->error("入职失败！");
                }
                $rs = $this->income_model->add(array(
                    'com_give'=>$com_info['com_give'],
                    'apply_job_id'=>$id,
                    'msg'=>$msg,
                    'created_time'=>time(),
                    'user_id'=>get_current_admin_id()
                ));
                if($rs!=false){
                    M()->commit();
                    $this->success("入职成功！");
                }else{
                    M()->rollback();
                    $this->error("入职失败！");
                }

           }else{
               M()->commit();
               $this->success("成功！");
           }
        }else{
            //回滚
            M()->rollback();
            $this->error("入职失败");
        }
    }
    
    /**
     * @info 管理员删除入职申请记录
     * @author Mojianguo
     * @date 2017年2月8日 22:27:16
     */
    public function deleteByAdmin(){
    	$id = I("get.id",0,'intval');
    	if($id==0){
    		$this->error("获取用户ID失败，请联系管理员");
    	}
    	//修改申请记录状态
    	$this->apply_job_model->last_time=time();
    	$this->apply_job_model->deleted_time=time();   //删除时间
    	$rs= $this->apply_job_model->where('id='.$id)->save();
    	if($rs!==false){
    			$this->success("操作成功！");
    	}else{
    		$this->error("删除失败");
    	}
    }
}
